// JavaScript Document
/********************************************************************
// 程序作者:		呆呆（L·IsSAc）
// 版权所有:		DoUNeed.com
// 技术支持:		issacdd@gmail.com
// 程序名称:		日历式日期选择控件
// 程序版本:		1.0
// 单元名称:		calendar.js
// 开始时间:		2005.10
// 最后修改:		2007.4.4
// 备注说明:		层架构；需要CSS配合
<link href="calendar.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript" src="calendar.js"></script>
<input id="input1" type="text" readonly="" onclick="Calendar.Get('input1')" value="2007-04-04" />
<img src="images/calendar.gif" onclick="Calendar.Get('input1')" />
********************************************************************/
/* 创建结构和对象 */
document.writeln("<div id=\"CalDiv\" class=\"calendar\"></div>");
document.writeln("<iframe id=\"CalFrm\" scrolling=\"no\" frameborder=\"0\" style=\"display:none; position:absolute;\"></iframe>");
var Calendar = new Object();
Calendar.Div = document.getElementById("CalDiv");
Calendar.Frm = document.getElementById("CalFrm");
Calendar.minYear = 1800;
Calendar.maxYear = 2500;
/* 取某年某月第一天的星期值 */
Calendar.GetFirstDayWeek = function (yy, mm) {
	return (new Date(yy, mm - 1, 1)).getDay();
};
/* 取某年某月的总天数 */
Calendar.GetNumOfDays = function (yy, mm) {
	return (new Date(yy, mm, 0)).getDate();
};
/* 上一年 */
Calendar.PrevYear = function (id, yy, mm, dd) {
	yy--;
	yy = (yy < Calendar.minYear) ? Calendar.maxYear : yy;
	dd = (Calendar.GetNumOfDays(yy, mm) < dd) ? Calendar.GetNumOfDays(yy, mm) : dd;
	Calendar.Show(id, yy, mm, dd);
};
/* 下一年 */
Calendar.NextYear = function (id, yy, mm, dd) {
	yy++;
	yy = (yy > Calendar.maxYear) ? Calendar.minYear : yy;
	dd = (Calendar.GetNumOfDays(yy, mm) < dd) ? Calendar.GetNumOfDays(yy, mm) : dd;
	Calendar.Show(id, yy, mm, dd);
};
/* 上一月 */
Calendar.PrevMonth = function (id, yy, mm, dd) {
	mm--;
	if (mm < 1) {
		mm = 12;
		yy--;
		yy = (yy < Calendar.minYear) ? Calendar.maxYear : yy;
	}
	dd = (Calendar.GetNumOfDays(yy, mm) < dd) ? Calendar.GetNumOfDays(yy, mm) : dd;
	Calendar.Show(id, yy, mm, dd);
};
/* 下一月 */
Calendar.NextMonth = function (id, yy, mm, dd) {
	mm++;
	if (mm > 12) {
		mm = 1;
		yy++;
		yy = (yy > Calendar.maxYear) ? Calendar.minYear : yy;
	}
	dd = (Calendar.GetNumOfDays(yy, mm) < dd) ? Calendar.GetNumOfDays(yy, mm) : dd;
	Calendar.Show(id, yy, mm, dd);
};
/* 输入框显示 */
Calendar.InputValue = function (id, yy, mm, dd) {
	mm = ((mm < 10) ? "0" : "") + mm;
	dd = ((dd < 10) ? "0" : "") + dd;
	document.getElementById(id).value = yy + "-" + mm + "-" + dd;
};
/* 判断日期(YYYY-MM-DD)是否正确 */
Calendar.DateIsTrue = function (value) {
	var date = value.split("-");
	var yy = parseFloat(date[0]);
	var mm = parseFloat(date[1]);
	var dd = parseFloat(date[2]);
	if (date.length != 3) {
		return false;
	}
	if ((date[0].length > 4) || (date[1].length > 2) || (date[2].length > 2)) {
		return false;
	}
	if (isNaN(yy) || isNaN(mm) || isNaN(dd)) {
		return false;
	}
	if ((yy < Calendar.minYear) || (yy > Calendar.maxYear)) {
		return false;
	}
	if ((mm > 12) || (mm <= 0)) {
		return false;
	}
	if (Calendar.GetNumOfDays(yy, mm) < dd) {
		return false;
	}
	return !isNaN(Date.UTC(yy, mm, dd));
};
/* 根据输入框中的日期显示日历（主调函数） */
Calendar.Get = function (id) {
	
	var yy, mm, dd;
	if (Calendar.DateIsTrue(document.getElementById(id).value)) {
		var date = document.getElementById(id).value.split("-");
//		var date2 = document.getElementById(id2).value.split("-");
		yy = parseFloat(date[0]);
		mm = parseFloat(date[1]);
		dd = parseFloat(date[2]);
		Calendar.Show(id, yy, mm, dd);
	} else {
		var today = new Date();
		yy = today.getFullYear();	//Moz下的getYear为实际年-1900
		mm = (today.getMonth() + 1);
		dd = today.getDate();
		Calendar.Show(id, yy, mm, dd);
	}
	if (document.getElementById("submit").disabled == false) {
		document.getElementById("submit").disabled = true;
		document.getElementById("reason").disabled = true;
		
	}
};
/* 显示日历 */
Calendar.Show = function (id, yy, mm, dd) {

	var currFDW = Calendar.GetFirstDayWeek(yy, mm);
	var nextFDW = Calendar.GetFirstDayWeek(yy, mm + 1);
	var prevNOD = Calendar.GetNumOfDays(yy, mm - 1);
	var currNOD = Calendar.GetNumOfDays(yy, mm);
	var o = document.getElementById(id);
	var left, top;
	var Content;
	var w = 0;
	var weekText = new Array("sun", "mon", "tue", "wed", "thu", "fri", "sat");	//星期文本

	//显示日历内容
	Content = "<div class=\"calHeader\">" + "\t<span class=\"calYear\">" + "\t\t<a class=\"calPrev\" href=\"javascript:;\" onclick=\"Calendar.PrevYear ('" + id + "'," + yy + "," + mm + "," + dd + ")\" title=\"\u4e0a\u4e00\u5e74\">&laquo;</a>" + "\t\t<span>" + yy + "\u5e74</span>" + "\t\t<a class=\"calNext\" href=\"javascript:;\" onclick=\"Calendar.NextYear ('" + id + "'," + yy + "," + mm + "," + dd + ")\" title=\"\u4e0b\u4e00\u5e74\">&raquo;</a>" + "\t</span>" + "\t<span class=\"calMonth\">" + "\t\t<a class=\"calPrev\" href=\"javascript:;\" onclick=\"Calendar.PrevMonth ('" + id + "'," + yy + "," + mm + "," + dd + ")\" title=\"\u4e0a\u4e00\u6708\">&laquo;</a>" + "\t\t<span>" + mm + "\u6708</span>" + "\t\t<a class=\"calNext\" href=\"javascript:;\" onclick=\"Calendar.NextMonth ('" + id + "'," + yy + "," + mm + "," + dd + ")\" title=\"\u4e0b\u4e00\u6708\">&raquo;</a>" + "\t</span>" + "\t<span>" + "\t\t<a class=\"calClose\" href=\"javascript:;\" onclick=\"Calendar.Close()\" title=\"\u5173\u95ed\">Close</a>" + "\t</span>" + "</div>";
	Content += "<div class=\"calWeek\">" + "\t<ul>";
	for (i = 0; i < 7; i++) {
		Content += "\t<li class=\"" + (((i % 7) == 0) ? "calSun" : (((i % 7) == 6) ? "calSat" : "calWork")) + "\">" + weekText[i] + "</li>";
	}
	Content += "\t</ul>" + "</div>";
	Content += "<div class=\"calDay\">" + "\t<ul>";
	if (currFDW != 0) {					//上一月日期
		for (i = (prevNOD - currFDW + 1); i <= prevNOD; i++) {
			Content += "<li class=\"calPrevMonth\">" + "\t<a href=\"javascript:;\" onclick=\"Calendar.PrevMonth ('" + id + "'," + yy + "," + mm + "," + i + ")\">" + i + "</a>" + "</li>";
			w++;
		}
	}
	for (i = 1; i <= currNOD; i++) {	//当前月日期
		Content += "\t<li " + ((i == dd) ? "id=\"calCurr\"" : "") + " class=\"" + (((w % 7) == 0) ? "calSun" : (((w % 7) == 6) ? "calSat" : "calWork")) + "\">" + "\t\t<a href=\"javascript:;\" onclick=\"Calendar.InputValue('" + id + "'," + yy + "," + mm + "," + i + "); Calendar.Close()\">" + i + "</a>" + "\t</li>";
		w++;
	}
	if (nextFDW != 0) {					//下一月日期
	}
	for (i = 1; i <= (7 - nextFDW); i++) {
		Content += "<li class=\"calNextMonth\">" + "\t<a href=\"javascript:;\" onclick=\"Calendar.NextMonth('" + id + "'," + yy + "," + mm + "," + i + ")\">" + i + "</a>" + "</li>";
	}
	Content += "\t</ul>" + "</div>";
	Calendar.Div.innerHTML = Content;
	
	//定义容器位置样式
	Calendar.Div.style.display = "block";
	Calendar.Div.style.zIndex = 1000;
	if (o.getBoundingClientRect) {			// For IE
		left = o.getBoundingClientRect().left;
		top = o.getBoundingClientRect().top + o.clientHeight;
	} else {
		if (document.getBoxObjectFor) {	// For Moz
			left = document.getBoxObjectFor(o).x + "px";
			top = document.getBoxObjectFor(o).y + o.clientHeight + "px";
		}
	}
	Calendar.Div.style.left = left;
	Calendar.Div.style.top = top;	
	
	//定义隐藏框架样式
	with (Calendar.Frm.style) {
		display = "block";
		zIndex = Calendar.Div.style.zIndex - 1;
		width = Calendar.Div.offsetWidth + "px";
		height = Calendar.Div.offsetHeight + "px";
		left = Calendar.Div.style.left;
		top = Calendar.Div.style.top;
	}
};
/* 关闭日历 */
Calendar.Close = function () {
	Calendar.Div.innerHTML = "";
	Calendar.Div.style.display = "none";
	Calendar.Frm.style.display = "none";
};

